﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.Forms
{
    public partial class frm_JustNewEdit : DevExpress.XtraEditors.XtraForm
    {
        public int Id;
        public DateTime Date;
        public string ToolId;
        public int Quantity;
        public int ToolOrQTool;
        public string Note;
        private ucJustNew parent;
        private DataTable dt = new DataTable();
        private int import;

        public frm_JustNewEdit(ucJustNew fJustNew)
        {
            InitializeComponent();
            parent = fJustNew;
            this.CancelButton = this.btTCancel;
        }

        private void rdTool_CheckedChanged(object sender, EventArgs e)
        {
            BindDataTool(ToolConstants.Configuration.ToolType.IsTool);
            lblToolOrQTool.Text = ToolConstants.Configuration.ToolType.IsTool.ToString();
        }

        private void rdQTool_CheckedChanged(object sender, EventArgs e)
        {
            BindDataTool(ToolConstants.Configuration.ToolType.IsQTool);
            lblToolOrQTool.Text = ToolConstants.Configuration.ToolType.IsQTool.ToString();
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            dtpTDate.Value = Date;
            txtTQuantity.Text = Quantity.ToString();
            memoTNote.Text = Note;

            if (ToolOrQTool == 0)
            {
                rdTool.Checked = true;
            }
            else
            {
                rdQTool.Checked = true;
            }

            try
            {
                cmbToolID.SelectedValue = ToolId;
                if (cmbToolID.SelectedItem == null)
                {
                    cmbToolID.SelectedItem = cmbToolID.Items[0];
                }
            }
            catch (Exception)
            {
                cmbToolID.SelectedItem = cmbToolID.Items[0];
            }

        }

        private void btTUpdate_Click_1(object sender, EventArgs e)
        {
            UpdateTool();
        }

        private void btTCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbToolID_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoMoi;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                import = Utilities.CheckInput(fieldName, storedName, where);
                MessageBox.Show(string.Format(ToolConstants.Message.JustNew.ImportQuantity, cmbToolID.SelectedValue, import.ToString()), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        #region Private methods

        private bool GetDataTool(JustNew o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.JustNew.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolID.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.JustNew.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.JustNew.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.JustNew.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoMoi;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                import = Utilities.CheckInput(fieldName, storedName, where);

                if (import < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.JustNew.WrongQuantity, cmbToolID.SelectedValue, import.ToString(), txtTQuantity.Text), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Text = "";
                    txtTQuantity.Focus();
                    return false;

                }

            }
            #endregion

            o.ID = Id;
            o.Date = dtpTDate.Value;
            o.ToolID = cmbToolID.SelectedValue.ToString();
            o.Quantity = int.Parse(txtTQuantity.Text);
            o.ToolOrQTool = int.Parse(lblToolOrQTool.Text);
            o.Note = memoTNote.Text;

            return true;
        }

        private void BindDataTool(int type)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            helper.ValueObject.ToolOrQTool = type;
            dt = helper.GetAllByToolType();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
            cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;


        }

        private void UpdateTool()
        {
            JustNewHelper helper = new JustNewHelper(new JustNew());
            if (GetDataTool(helper.ValueObject))
            {

                Utilities.Timer timer = new Utilities.Timer();

                if (helper.Update() > 0)
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
                }
                else
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
                }
                parent.BindDatatoGrid();

            }
        }

        #endregion






    }
}